package com.gmall.jdk8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest1 {

    public static void main(String[] args) {
        test_终止操作2();
    }
    public static void test_生成流(){
        //1.Collection/List 接口
        //2.Arrays 类中的静态方法
        //3.Stream 接口的静态方法 of
        //4.Stream 接口的静态方法 gen
        List<Integer> list1 = new ArrayList<>();
        list1.add(4);
        list1.add(3);
        list1.add(7);
        list1.add(2);
        list1.add(3);

        Stream<Integer> stream = list1.stream();

        Integer[] array1 = new Integer[]{4,3,7,2,3};
        Stream<Integer> stream1 = Arrays.stream(array1);

        Stream<Integer> stream2 = Stream.of(4, 3, 7, 2, 3);

        //接口 a = ()->{}
        Stream<Double> generate = Stream.generate(() -> {
            return Math.random();
        });
    }

    public static void test_中间操作() {
        Integer[] array1 = new Integer[]{4, 3, 7, 2, 3, 10, 18, 17, 6, 4};
        Arrays.stream(array1)
                .filter((a) -> {
                    return a >= 3 ? true : false;
                }).distinct()
                .sorted((o1, o2) -> {
                    return o2 - o1;
                }).skip(2)
                .limit(3)
                .map((t) -> {
                    return "=======" + t * t;
                })
                .forEach((b1) -> {
                    System.out.println(b1);
                });
    }

    public static void test_终止操作() {
        Integer[] array1 = new Integer[]{4,3,7,2,3,10,18,17,6,4};
        List<String> collect = Arrays.stream(array1)
                .filter((a) -> {
                    return a >= 3 ? true : false;
                }).distinct()
                .sorted((o1, o2) -> {
                    return o2 - o1;
                }).skip(1)
                .limit(4)
                .map((t) -> {
                    return "=======" + t * t;
                }).collect(Collectors.toList());

        collect.forEach(a->System.out.println(a));
    }

    public static void test_终止操作1() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("zhangsan",23,80.5,"2109A"));
        list.add(new Student("lisi",24,68.0,"2109A"));
        list.add(new Student("wangwu",22,80.5,"2109B"));
        list.add(new Student("zhangsan2",21,75.5,"2109B"));
        list.add(new Student("zhangsan1",22,70.5,"2109B"));

        list.stream().filter(s ->{
            return s.getAge() >19;
            })/*.peek((a)->{
                a.setAge(a.getAge()+1);
            })*/.map(a->{//Map
               GraducateStudent graducateStudent = new GraducateStudent(a.getName(),a.getAge());
                graducateStudent.setTime(new Date());
                graducateStudent.setAge(a.getAge()+1);
                return graducateStudent ;
            })
            .forEach(s -> System.out.println(s));
    }

    public static void test_终止操作2() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("zhangsan",23,80.5,"2109A"));
        list.add(new Student("lisi",24,68.0,"2109A"));
        list.add(new Student("wangwu",22,80.5,"2109B"));
        list.add(new Student("zhangsan2",21,75.5,"2109B"));
        list.add(new Student("zhangsan1",22,70.5,"2109B"));

        String collect = list.stream().filter(s -> {
                    return s.getAge() > 19;
                }).map(a -> {//Map
                    return a.getName();
                })
                .collect(Collectors.joining(","));

        System.out.println(collect);
    }
}
